﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using exsportance.Silverlight.Common.Controls;
using exsportance.Silverlight.Common.Helper;
using System.Collections.Generic;

namespace exsportance.Silverlight.Common.Utils
{
    public static class DebugUtils
    {
        #region members
        private static List<string> _tmpMessages = new List<string>();
        #endregion

        #region public methods
        /// <summary>
        /// Adds a Listbox to the main page
        /// </summary>
        public static void AddOutputWindow()
        {
            if (Application.Current.RootVisual != null)
            {
                Panel parent = getRootElement(Application.Current.RootVisual);

                if (parent != null)
                {

                    DebugWindow window = new DebugWindow();
                    window.Name = "DebugWindow";
                    window.Width = double.NaN; // auto
                    window.Height = 100;
                    window.VerticalAlignment = VerticalAlignment.Bottom;
                    parent.Children.Add(window);

                    // set location to bottom
                    if (parent is Canvas)
                    {
                        window.SetValue(Canvas.LeftProperty, 0);
                        window.SetValue(Canvas.TopProperty, parent.Height - window.Height);
                    }
                    else
                        if (parent is Grid)
                        {
                            Grid grid = parent as Grid;
                            if (grid.ColumnDefinitions.Count > 0)
                            {
                                window.SetValue(Grid.ColumnProperty, 0);
                                window.SetValue(Grid.ColumnSpanProperty, grid.ColumnDefinitions.Count);
                            }

                            if (grid.RowDefinitions.Count > 0)
                            {
                                window.Height = double.NaN; // auto
                                window.SetValue(Grid.RowProperty, grid.RowDefinitions.Count - 1);
                            }
                        }

                    // write remembered messages
                    foreach (string s in _tmpMessages)
                        window.WriteLine(s);

                    // clear messages
                    _tmpMessages.Clear();
                }
            }
        }

        public static void WriteLine(string message)
        {
            if (UISynchronizationContext.Instance != null && UISynchronizationContext.Instance.IsValid)
                UISynchronizationContext.Instance.InvokeAsynchronously(() => WriteLineCore(message));
            else
                _tmpMessages.Add(message);
        }
        #endregion

        #region private methods
        private static Panel getRootElement(UIElement element)
        {
            Panel result = null;

            if (element is UserControl)
            {
                UserControl mainPage = (element as UserControl);
                object root = mainPage.FindName("LayoutRoot");

                if (root != null && root is Panel)
                    result = root as Panel;
            }
            else
                if (element is Panel)
                    result = element as Panel;

            return result;
        }

        private static void WriteLineCore(string message)
        {
            if (Application.Current.RootVisual != null)
            {
                Panel parent = getRootElement(Application.Current.RootVisual);

                if (parent != null)
                {
                    object element = parent.FindName("DebugWindow");

                    if (element == null)
                    {
                        foreach (UIElement child in parent.Children)
                        {
                            if (child is DebugWindow)
                            {
                                element = child;
                                break;
                            }
                            else
                            {
                                if (child is FrameworkElement)
                                    element = (child as FrameworkElement).FindName("DebugWindow");

                                if (element != null)
                                    break;
                            }
                        }
                    }

                    if (element != null)
                    {
                        DebugWindow window = element as DebugWindow;
                        window.WriteLine(message);
                    }
                    else
                        _tmpMessages.Add(message);

                }
            }
            else
                _tmpMessages.Add(message);
        }
        #endregion
    }
}
